clear all
set more off

set varabbrev off

******************************************************************************
* Specify working directory
******************************************************************************

* For example
* cd "/Users/.../XYZ/"

global rawdata "Data/Raw_Data"
global tempdata "Data/RevisionRFS/temp_data"
global masterdata "Data/Master_Data"
global revisiondata "Data/RevisionRFS"

clear all
set more off
set type double

cap log close

global figures = "Results/Figures"
global output = "Results/Output"
global revisionoutput = "Results/RevisionRFS/Output"

****************************************************************************************************
//Second Stage
****************************************************************************************************

/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_std.dta", clear

merge n:1 issuer_id_str date using "${tempdata}/financial_ratios_compustat.dta"
keep if _merge ==3
drop _merge

foreach var of varlist debt_assets{

	bys type_new2: egen `var'_sd = sd(`var')
	replace `var' = `var'/`var'_sd
	drop `var'_sd
 
}

label var debt_assets "$\textit{Leverage}_{n,t}$" 

********************************************************************************
/* 1. Step: Pooled Regression */
********************************************************************************

/* Estimation */
forval i=1(1)7{
preserve
keep if type_new2==`i'

eststo: ivreghdfe LNrweight (yield = IVme_alt6_95) LNtimetomat spread LNbv debt_assets coupon_dec if type_new2==`i' [aw=aum_tot], a(date#fundid) clus(fundid) nocollin 
 	estadd local fund_time_fe "Yes"
	
restore
}

/* Reporting */
esttab using "${revisionoutput}/pooled_results_alt6_robustness_lr2", replace f booktabs ///
b(%9.3f) se(%9.3f) ar2(%9.3f) eqlabels(none) title(xx) ///
mgroups("Life" "P\&C" "Traditional" "Variable Annuity" "ETF" "Others \& Pension" "Foreign", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(fund_time_fe N r2_a widstat, fmt(0 %15.0gc %9.2f %9.2f) ///
labels("Fund $\times$ Quarter Fixed Effects" "Observations" "Kleibergen-Paap F-statistic")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
keep(yield LNtimetomat spread LNbv debt_assets) ///
obslast label ///
addnotes(Standard errors are clustered at the bond level) ///
nonumbers mtitle("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X" "XI" "XII") ///
substitute(\_ _)
eststo clear

/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_std.dta", clear

merge n:1 issuer_id_str date using "${tempdata}/financial_ratios_compustat.dta"
keep if _merge ==3
drop _merge

foreach var of varlist debt_assets{

	bys type_new2: egen `var'_sd = sd(`var')
	replace `var' = `var'/`var'_sd
	drop `var'_sd
 
}

label var debt_assets "$\textit{Leverage}_{n,t}$" 

********************************************************************************
/* 2. Step: Within Assetclass Regressions - Pooled First Stage */
********************************************************************************

drop yieldhat

gen yieldhat = .

forval i=1(1)7{
	
	reghdfe yield IVme_alt6_95 LNtimetomat spread LNbv debt_assets coupon_dec if type_new2==`i' [aw=aum_tot], a(date#fundid) clus(fundid) resid
	
	predict yieldhat_type, xbd
	
	replace yieldhat = yieldhat_type if type_new2==`i'
	
	drop yieldhat_type

}

label var yieldhat "$\overline{\textit{Yield}}_{n,t}$"  

/* Estimation */
forval i=1(1)7{
preserve
	keep if type_new2==`i' 
	
	eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv debt_assets coupon_dec if type_new2==`i' & assetclass == 1 [aw=aum_assetclass_ac], a(date#fundid_ac) clus(fundid_ac) resid
	estadd local fund_time_fe "Yes"
	
	predict deltahat_1, xbd
	
	replace deltahat_1 =. if assetclass == 2
	
	eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv debt_assets coupon_dec if type_new2==`i' & assetclass == 2 [aw=aum_assetclass_ac] , a(date#fundid_ac) clus(fundid_ac) resid
 	estadd local fund_time_fe "Yes"
	
	predict deltahat_2, xbd
	
	replace deltahat_2 =. if assetclass == 1
	
	gen LNdeltahat = deltahat_1
	replace LNdeltahat = deltahat_2 if LNdeltahat ==. 
	
	keep date fundid bondid assetclass LNdeltahat type_new2
	
// 	save "${tempdata}/TMP_`i'_conditional_alt6_pooled_fs_robustness_lr2", replace

restore
}

/* Reporting */
esttab using "${revisionoutput}/conditional_results_alt6_pooled_fs_robustness_lr2", replace f booktabs ///
b(%9.3f) se(%9.3f) ar2(%9.3f) eqlabels(none) title(xx) ///
mgroups("Life" "P\&C" "Traditional" "Variable Annuity" "ETF" "Others \& Pension" "Foreign", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(fund_time_fe N r2_a widstat, fmt(0 %15.0gc %9.2f %9.2f) ///
labels("Fund $\times$ Quarter Fixed Effects" "Observations" "Kleibergen-Paap F-statistic")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
keep(yieldhat LNtimetomat spread LNbv debt_assets) ///
obslast label ///
addnotes(Standard errors are clustered at the bond level) ///
nonumbers mtitle("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X" "XI" "XII") ///
substitute(\_ _)
eststo clear

preserve
